// miniprogram/pages/my/index.js
var that
const DB = wx.cloud.database()
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab_num: 1,
    userStatus: 0, //0 初始     1 信息都有    2 有微信信息，没个人信息    3 有微信信息，有个人信息，没三观
    guanli: false,
  },
  renzheng() {
    if (!this.data.userInfo.examineStatus) {
      wx.navigateTo({
        url: '../renzheng/renzheng',
      })
    }
  },
  //用户授权登录登录
  async loginGetUserInfo(e) {
    let get_user = await wx.cloud.callFunction({name: "get_user"});
    console.log('get_user-------',get_user)
    wx.getUserInfo({ //调用授权api，获取用户信息
      success: function (res) {
        app.AppUserAdd(res.userInfo,'nav')
      }
    })
  },
  onLoad() {
    that = this
  },
  getMyInfo(_openid) {
    wx.setStorageSync('_openid', _openid)
    DB.collection('user').where({
        _openid: _openid
      })
      .get().then(ress => {
        console.log('getMyInfo', _openid, ress)
        if (ress.data.length > 0) {
          let userInfo = ress.data[0];
          // 数据库有我，
          wx.setStorageSync('userInfo', userInfo)

          if (!userInfo.userInfoDetail.age) {
            that.setData({
              userStatus: 2
            })
            return
          }

          if (userInfo.userInfoDetail.age && !userInfo.character_list) {
            that.setData({
              userStatus: 3
            })
            return
          }
          console.log('res', ress.data)
          var tiwenNum = userInfo.tiwenNum;
          var seeNum = userInfo.seeNum;
          var zan_openid = userInfo.zan_openid ? userInfo.zan_openid.length : 0;
          var renqi = tiwenNum + seeNum + zan_openid;

          if (getApp().guanli_list_openid.indexOf(userInfo._openid) != -1) {
            this.setData({
              guanli: true
            })
          } else {
            this.setData({
              guanli: false
            })
          }
          this.setData({
            userStatus: 1,
            userInfo: userInfo,
            renqi
          })
        } else {
          that.setData({
            userStatus: 0
          })
        }
      })

    // 【我的】-【邀请记录】-【邀请我的】中，有未处理的邀请记录，就在【邀请记录】上显示小红点
    DB.collection('invitation_list').where({
        b: _openid,
      })
      .get().then(ress => {
        console.log('呗邀请的', ress)
        ress.data.forEach(item => {
          if (item.status != 1 && item.status != 2) {
            this.setData({
              hongdian: true
            })
          }
        })
      })

  },
  onShow() {
    if (wx.getStorageSync('userInfo')) {
      that.getMyInfo(wx.getStorageSync('userInfo')._openid)
    } else {
      wx.cloud.callFunction({
        name: "openId",
        data: {},
        success(res) {
          console.log('个人信息-------------', res.result.openid)
          that.getMyInfo(res.result.openid)
        },
        fail(err) {
          console.log('个人信息-------------', err)

        }
      })
    }

  },
  click_tab(e) {
    getApp().sendMsg();
    console.log('eeeeeee', e.currentTarget.dataset.nav)
    var nav = e.currentTarget.dataset.nav;
    if (nav == 1) {
      wx.switchTab({
        url: '../friends_list/friends_list',
      })
    } else if (nav == 2) {
      wx.navigateTo({
        url: '../invitation_list/invitation_list',
      })
    } else if (nav == 3) {
      wx.navigateTo({
        url: '../shoucang/index',
      })
    } else if (nav == 4) {
      wx.navigateTo({
        url: '../info/index',
      })
    } else if (nav == '2_2') {
      wx.navigateTo({
        url: '../quiz_list/quiz_list',
      })
    } else if (nav == '2_1') {
      wx.navigateTo({
        url: '/pages/administration/shenhe/shenhe',
      })
    }
  },
  // 收藏
  shoucang(e) {
    getApp().sendMsg();
    wx.navigateTo({
      url: 'shoucang/index',
    })
  },

  onShareAppMessage: function () {
    var res = {
      path: "/pages/user_detail/user_detail?openid=" + wx.getStorageSync('userInfo')._openid + '&share=true',
      success: function (e) {},
      // title: getApp().share_title || null,
      imageUrl: wx.getStorageSync('userInfo').userInfoDetail.live_phone[0]
    };
    return res;
  },
})